package com.doradance.app.home;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.account.ParamKey;
import com.doradance.account.UrlCenter;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.FreeItemEntity;
import com.doradance.entity.IndexEntity;
import com.example.xlhratingbar_lib.XLHRatingBar;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class FreeListFragment extends SwipeListFragment {

    private int mPage = 0;
    public FreeListFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
//        startLoading();

        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new FreeListAdapter(getActivity());
    }

    private ListDataRequestListener<FreeItemEntity> mResponceListener = new ListDataRequestListener<>(this, FreeItemEntity.class, false);

    @Override
    public void onStartLoading() {
        if (mPage==0||mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
            MyHttpRequest request = new MyHttpRequest(getContext());
            UrlParams urlParams = new UrlParams();
            urlParams.put(ParamKey.PAGE, mPage + "");
            urlParams.put(ParamKey.SIZE, 12 + "");
            request.get(UrlCenter.FREE_LIST, urlParams, mResponceListener);
        }
        }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
        FreeItemEntity o = (FreeItemEntity) getData().get(position);
        CoursesDetailActivity.start(getActivity(),o.getId());
    }

    @Override
    public void onResume() {
        super.onResume();
        getData().clear();
        getAdapter().notifyDataSetChanged();
        mPage = 0;
        startLoading();
        clearDecorations();
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        mPage = 0;
        startLoading();
    }


}
